﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TwoPageView3 {
    /// <summary>
    /// viewmodel base class of treeview item from "Simplifying the WPF TreeView by Using the ViewModel Pattern" in codeproject
    /// </summary>
    public class FileSystemTreeViewItemViewModel : INotifyPropertyChanged {
        private static readonly FileSystemTreeViewItemViewModel mDummyChild = new FileSystemTreeViewItemViewModel();
        private readonly FileSystemTreeViewItemViewModel mParent;
        private ObservableCollection<FileSystemTreeViewItemViewModel> mChildren;
        private readonly string mDisplayName;
        private readonly string mActualName;
        private bool mIsExpanded;
        private bool mIsSelected;

        public ObservableCollection<FileSystemTreeViewItemViewModel> Children {
            get { return mChildren; }
        }

        public FileSystemTreeViewItemViewModel Parent { get { return mParent; } }
        public string DisplayName { get { return mDisplayName; } }
        public string ActualName { get { return mActualName; } }

        public bool HasDummyChild {
            get { return Children.Count == 1 && Children[0] == mDummyChild; }
        }

        protected FileSystemTreeViewItemViewModel(string actualName, string displayName, FileSystemTreeViewItemViewModel parent) {
            mActualName  = actualName;
            mDisplayName = displayName;
            mParent = parent;
        }

        private FileSystemTreeViewItemViewModel() {
        }

        protected void InitChildren(bool bAddDummyChild) {
            mChildren = new ObservableCollection<FileSystemTreeViewItemViewModel>();

            if (bAddDummyChild) { 
                mChildren.Add(mDummyChild);
            }
        }

        public bool IsExpanded {
            get { return mIsExpanded; }
            set {
                if (value != mIsExpanded) {
                    mIsExpanded = value;
                    OnPropertyChanged("IsExpanded");
                }

                // Expand all the way up to the root.
                if (mIsExpanded && mParent != null) {
                    mParent.IsExpanded = true;
                }

                // Lazy load the child items, if necessary.
                if (HasDummyChild) {
                    Children.Remove(mDummyChild);
                    LoadChildren();
                }
            }
        }

        public bool IsSelected {
            get { return mIsSelected; }
            set {
                if (value != mIsSelected) {
                    mIsSelected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName) {
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        protected virtual void LoadChildren() {
        }

        public string MakePath() {
            var sb = new StringBuilder();
            sb.Append(mActualName);
            if (Parent != null) {
                mParent.MakePathRecursive(sb);
            } else {
                sb.Append(System.IO.Path.DirectorySeparatorChar);
            }
            return sb.ToString();
        }

        private void MakePathRecursive(StringBuilder sb) {
            sb.Insert(0, System.IO.Path.DirectorySeparatorChar);
            sb.Insert(0, mActualName);
            if (Parent == null) {
                return;
            }

            mParent.MakePathRecursive(sb);
        }
    }
}
